<?php
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';

$url = MiscUtils::getParam('url', NULL);
$dirname = basename(dirname(__FILE__));

$tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'user_profile', NULL, NULL, NULL, 1, 1, NULL)->d;
$page_title = '';
if (count($tmp) > 0) {
  $profile = $tmp[0];
  $page_title = $tmp[0]->shop_name;
}

if (isset($_COOKIE['my_cart'])) {
  $_SESSION['my_cart'] = json_decode($_COOKIE['my_cart']);
}

if (!isset($_SESSION['my_cart'])) {
  $_SESSION['my_cart'] = array();
}

$root = new stdClass();
$root->id = 0;
$root->title = 'ROOT';
__root($pages, $root, false);

$root2 = new stdClass();
$root2->id = 0;
$root2->title = 'ROOT2';
$pages = json_decode(json_encode($pages));
__root($pages, $root2, true);
$nav = new stdClass();
$nav->data = array();
__nav($root2, $page_id, $nav);

$high_id = 0;
if ($page->parent_id == 0 && $page->is_visible == false) {
  foreach ($pages as &$obj) {
    if ($obj->is_default == 1) {
      $high_id = $obj->id;
    }
  }
} else {
  foreach ($root2->c as $child) {
    $high_id = __high($child, $page_id, $child->id);
    if ($high_id != 0) {
      break;
    }
  }
}

function __root($a, $n, $inc_all) {
  foreach ($a as $idx => &$elm) {
    if ($elm->is_visible == 0 && $inc_all == false) {
      continue;
    }
    if ($elm->parent_id == $n->id) {
      $elm->c = array();
      $n->c[] = $elm;
      __root($a, $elm, $inc_all);
    }
  }
}

function __nav($n, $page_id, $nav) {
  if ($n->id == $page_id) {
    return true;
  }
  foreach ($n->c as $child) {
    array_push($nav->data, $child);
    $rslt = __nav($child, $page_id, $nav);
    if ($rslt) {
      return true;;
    }
    array_pop($nav->data);
  }
  return false;
}

function __display($node, $high_id) {
  if ($node->id != 0) {
    if ($node->parent_id == 0 && $node->id == $high_id) {
      echo "<li class=\"menu hi\"><a " . ($node->parent_id == 0 ? 'class="fNiv"' : '') . "href=\"?i=$node->id\">$node->title</a>";
    } else if ($node->parent_id == 0) {
      echo "<li class=\"menu\"><a " . ($node->parent_id == 0 ? 'class="fNiv"' : '') . "href=\"?i=$node->id\">$node->title</a>";
    } else {
      echo "<li><a " . ($node->parent_id == 0 ? 'class="fNiv"' : '') . "href=\"?i=$node->id\">$node->title</a>";
    }
  }
  if (count($node->c) > 0 && $node->id != 0) {
    echo '<ul>';
  } else if ($node->id != 0) {
    echo '</li>';
  }
  foreach ($node->c as $child) {
    __display($child, $high_id);
  }
  if (count($node->c) > 0 && $node->id != 0) {
    echo '</ul></li>';
  }
}

function __high($node, $page_id, $high_id) {
  if ($node->id == $page_id) {
    return $high_id;
  }
  foreach ($node->c as $child) {
    $rslt = __high($child, $page_id, $high_id);
    if ($rslt != 0) {
      return $rslt;
    }
  }
  return 0;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title id="page_title"><?php echo $page_title . ' | ' . $page->title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="exts/uis/<?php echo $dirname; ?>/jMenu/css/jMenu.jquery.css" />
<script type="text/javascript" src="libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" language="javascript" src="exts/uis/<?php echo $dirname; ?>/jMenu/js/jquery-ui.js"></script>
<script type="text/javascript" language="javascript" src="exts/uis/<?php echo $dirname; ?>/jMenu/js/jMenu.jquery.js"></script>
<style>
* {
  font-family: sans-serif;
  font-size: 12px;
  outline: 0;
  margin: 0;
}
body {
  text-align: center;
}
.toolbar {
  height: 25px;
  background: #ddd;
  border-bottom: 1px solid #888;
}
.toolbar div {
  width: 950px;
  margin: 0 auto;
}
.toolbar ul {
  float: right;
}
.toolbar li {
  display: inline;
  line-height: 22px;
  vertical-align: middle;
}
.toolbar a {
  color: #000;
  font-family: Lucida Sans Unicode;
  text-decoration: none;
  margin: 0 5px;
}
.toolbar a:hover {
  text-decoration: underline;
}
div.menubar {
}
div.btmline {
  height: 2px;
  background: #ff8000;
}
ul.nav {
  padding: 0;
  line-height: 22px;
  vertical-align: middle;
  margin-top: 5px;
}
ul.nav li {
  display: inline;
}
ul.nav li.gap {
  padding: 0 6px;
}
ul.nav a {
  font-family: Trebuchet MS;
  text-decoration: none;
  color: #333;
}
ul.nav a:hover {
  text-decoration: underline;
  color: #666;
}
<?php echo $page->css; ?>
</style>
</head>
<body>
  <div class="toolbar">
    <div>
      <?php if (isset($_SESSION[$sys_script . USER]) && $_SESSION[$sys_script . USER]->id) { ?>
      <ul>
        <li><a href="?i=<?php echo HOME_PROD_ID; ?>&script=index_cart">Shoppint Cart <span <?php if (count($_SESSION['my_cart']) > 0) echo 'style="color: red;"'; ?>>(<?php echo count($_SESSION['my_cart']); ?>)</span></a></li>
        <li>|</li>
        <li><a href="?i=<?php echo ACCOUNT_ID; ?>">My Account</a> (<?php echo $_SESSION[$sys_script . USER]->_member; ?>)</li>
        <li>|</li>
        <li><a href="<?php echo MiscUtils::curPageURL(); ?>&logout=1">Logout</a></li>
      </ul>
      </li>
      <?php } else { ?>
      <ul>
        <li><a href="?i=<?php echo HOME_PROD_ID; ?>&script=index_cart">Shoppint Cart <span <?php if (count($_SESSION['my_cart']) > 0) echo 'style="color: red;"'; ?>>(<?php echo count($_SESSION['my_cart']); ?>)</span></a></li>
        <li>|</li>
        <li><a href="?i=<?php echo LOGIN_ID; ?>&url=<?php echo urlencode(preg_replace('/&url=(.*)/', '',MiscUtils::curPageURL())); ?>">Login</a></li>
        <li>|</li>
        <li><a href="?i=<?php echo LOGIN_ID; ?>&init=register&url=<?php echo urlencode(preg_replace('/&url=(.*)/', '',MiscUtils::curPageURL())); ?>">Sign up</a></li>
      </ul>
      <?php } ?>
    </div>
  </div>
  <div>&nbsp;</div>
  <div style="width: 950px; margin: 0 auto; text-align: left;min-height: 500px;">
    <div><img src="exts/uis/<?php echo $dirname; ?>/images/logo.jpg" style="height: 50px;"></img></div>
    <div>&nbsp;</div>
    <div class="menubar">
      <ul id="jMenu"><?php echo __display($root, $high_id); ?></ul>
    </div>
    <div class="btmline"></div>
    <?php if (count($nav->data) > 1) { ?>
    <ul id="<?php echo NAV; ?>" class="nav">
      <?php foreach ($nav->data as $idx => &$data) { ?>
      <li><a href="?i=<?php echo $data->id; ?>"><?php echo $data->title; ?><?php if ($idx == count($nav->data) - 1) { ?><span id="<?php echo LAST_NAV; ?>"></span><?php } ?></a></li>
      <?php if ($idx < count($nav->data) - 1) { ?>
      <li class="gap">&#8250;</li>
      <?php } ?>
      <?php } ?>
    </ul>
    <?php } ?>
    <div>&nbsp;</div>
    <?php echo $page->html; ?>
  </div>
  <div id="my_footer">
    <div>&nbsp;</div>
    <div style="width: 100%;text-align: center;background: #ddd;border-top: 1px solid #999;">
      <div style="width: 950px; margin: 0 auto; text-align: left;"><?php include dirname(__FILE__) . '/inc.help.php'; ?></div>
      <div style="width: 950px; margin: 0 auto; text-align: left;"><?php echo str_replace('../../../', '', $profile->footer_content); ?></div>
    </div>
  </div>
  <script type="text/javascript">
    <?php echo $page->js; ?>
    $(document).ready(function() {
      $("#jMenu").jMenu({
        absoluteTop: 25,
        ulWidth: 150
      });
    });
  </script>
</body>
</html>
